<?php
namespace app\index\controller;

use app\Basic;
use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use think\facade\Db;
use think\facade\Config;

class Index extends Basic
{
    public function index(){
        return $this->success();
    }
    public function upload()
    {
        $file = $this->request->file('file');
        $savename = \think\facade\Filesystem::putFile( 'upload', $file);
        return $this->success('上传成功',[
            'savename'=>$savename,
            '$file'=>$file,
        ]);
    }
    public function configs()
    {
        $list = Db::name('config')->select()->toArray();
        $configs = [];
        foreach ($list as $key => $item) {
            $configs[$item['name']]=$item['value'];
        }
        return $this->success('请求成功！',[
//            '$list'=>$list,
            'configs'=>$configs,
        ]);
    }

    public function sendsms()
    {
        if (empty($this->param['tel'])) {
            return $this->ajax_return(0, '发送失败,手机号不能为空');
        }
        if (empty($this->param['type'])) {
            return $this->ajax_return(0, '发送失败,type不能为空');
        }
        $config = [
            'accessKeyId' => Config::get('accessKeyId'),
            'accessKeySecret' => Config::get('accessKeySecret'),
        ];
        $client = new Client($config);
        $sendSms = new SendSms;
        $sendSms->setPhoneNumbers($this->param['tel']);
        $sendSms->setSignName(Config::get('SignName'));
        $sendSms->setTemplateCode(Config::get('TemplateCode'));
        $code = rand(1000, 9999);
        $sendSms->setTemplateParam(['code' => $code]);
        $sendSms->setOutId($this->param['tel']);
        $result = $client->execute($sendSms);
        if ($result->Code == 'OK') {
            Db::name('code')->insert([
                'tel' => $this->param['tel'],
                'code' => $code,
                'type' => $this->param['type'],
                'add_time' => time(),
            ]);
            return $this->success( '短信发送成功', []);
        } else {
            return $this->error( '短信发送失败', ['$result' => $result]);
        }
    }

    public function douyin_pay_notify()
    {
        try{
            $msgList = json_decode($this->param['msg'], true);
//            Db::name('debug')->insertGetId([
//                'msg'       =>'抖音支付回调',
//                'data'=>json_encode($this->param),
//                'add_time'=>time(),
//                'orderno' =>  $msgList['cp_orderno']
//            ]);

            $orderInfo = Db::name('charge_order')->where('order_sn', $msgList['cp_orderno'])->find();
            if($orderInfo&&$orderInfo['is_pay']){
                return json([
                    'err_no'=>0,
                    'err_tips'=>'success',
                ]);
            }
            if($orderInfo){
                Db::name('charge_order')->where('order_sn', $msgList['cp_orderno'])->update(['is_pay'=>1]);

                $chargeOption = json_decode($orderInfo['charge_option'], true);
                $userInfo = Db::name('user')->where('user_id', $orderInfo['user_id'])->find();
                $vmoney = $chargeOption['v_money'] + $chargeOption['v_money_give'];
                $afterVmoney = bcadd($userInfo['v_money'], $vmoney, 2);

                Db::name('user')->where('user_id', $orderInfo['user_id'])->update(['v_money'=>$afterVmoney]);
                Db::name('vmoneylog')->insert([
                    'user_id'       =>  $orderInfo['user_id'],
                    'log_type'      =>  1,
                    'up_or_down'    =>  1,
                    'change_num'    =>  $vmoney,
                    'before_num'    =>  $userInfo['v_money'],
                    'after_num'     =>  $afterVmoney,
                    'title'         =>  '充值',
                    'add_time'      =>  time()
                ]);

                if($userInfo['share_id']){//有上级的话需要处理代理分销，
                    $agent_info = Db::name('user')->where('user_id', $userInfo['share_id'])->find();
                    if($agent_info['is_agent']){
                        Db::name('user')->where('user_id', $agent_info['user_id'])->update(['money'=>$agent_info['money']+$agent_info['agent_rate']*$orderInfo['pay_money'] ]);
                        Db::name('moneylog')->insert([
                            'user_id'       =>  $agent_info['user_id'],
                            'log_type'      =>  1,
                            'up_or_down'    =>  1,
                            'change_num'    =>  $agent_info['agent_rate']*$orderInfo['pay_money'],
                            'before_num'    =>  $agent_info['money'],
                            'after_num'     =>  $agent_info['money']+$agent_info['agent_rate']*$orderInfo['pay_money'],
                            'title'         =>  '用户:'.$userInfo['nickname'].'充值,获得收益',
                            'add_time'      =>  time()
                        ]);
                    }

                    if($agent_info['share_id']){//代理有上级的话需要处理团长分销，
                        $leader_info = Db::name('user')->where('user_id', $agent_info['share_id'])->find();
                        if($leader_info['is_leader']){
                            Db::name('user')->where('user_id', $leader_info['user_id'])->update(['money'=>$leader_info['money']+$leader_info['leader_rate']*$orderInfo['pay_money'] ]);
                            Db::name('moneylog')->insert([
                                'user_id'       =>  $leader_info['user_id'],
                                'log_type'      =>  1,
                                'up_or_down'    =>  1,
                                'change_num'    =>  $leader_info['leader_rate']*$orderInfo['pay_money'],
                                'before_num'    =>  $leader_info['money'],
                                'after_num'     =>  $leader_info['money']+$leader_info['leader_rate']*$orderInfo['pay_money'],
                                'title'         =>  '用户:'.$userInfo['nickname'].'充值,通过'.$agent_info['nickname'].'代理,获得收益',
                                'add_time'      =>  time()
                            ]);
                        }
                    }
                }
            }
        } catch (\Exception $e){
            Db::name('debug')->insertGetId([
                'msg'       =>  '抖音支付回调-Exception',
                'data'      =>  'msg: '.$e->getMessage() . ' line: '.$e->getLine(),
                'add_time'  =>  time()
            ]);
        }

        return json([
            'err_no'=>0,
            'err_tips'=>'success',
        ]);
    }



}
